package com.yanqu.road.server.manager.activity.hsisland;

import com.yanqu.road.dao.impl.activity.hsisland.HsIslandBattleFieldDamageDataDaoImpl;
import com.yanqu.road.entity.activity.hsisland.config.*;
import com.yanqu.road.entity.activity.hsisland.data.*;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandBattleFieldEnemyEnum;
import com.yanqu.road.server.manager.activity.hsisland.rank.CrossHsIslandRankListModel;
import com.yanqu.road.server.manager.activity.hsisland.rank.HsIslandRank;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.yanqu.road.entity.activity.hsisland.enums.HsIslandBattleFieldEnemyEnum.ENEMY_BOSS;

/**
 * 战场伤害数据
 */
public class CrossHsIslandBattleFieldDamage {

    /**
     * 伤害数据
     */
    private Map<Long, HsIslandBattleFieldDamageData> damageDataMap = new ConcurrentHashMap<>();

    /**
     * 本战场商会伤害榜单
     */
    public CrossHsIslandRankListModel unionDamageRankModel = null;

    /**
     * 战场id
     */
    private long battleFieldId;

    private int monsterId;

    private int activityId;

    private int groupId;

    private HsIslandBattleFieldEnemyEnum enemyType;


    public CrossHsIslandRankListModel getUnionDamageRankModel() {
        return unionDamageRankModel;
    }

    public long getBattleFieldId() {
        return battleFieldId;
    }

    public int getActivityId() {
        return activityId;
    }

    public int getGroupId() {
        return groupId;
    }

    public int getMonsterId() {
        return monsterId;
    }

    /**
     * 构造
     */
    public CrossHsIslandBattleFieldDamage(int activityId, int groupId, long battleFieldId, int monsterId, HsIslandBattleFieldEnemyEnum enemyType) {
        this.activityId = activityId;
        this.groupId = groupId;
        this.battleFieldId = battleFieldId;
        this.monsterId = monsterId;
        this.enemyType = enemyType;
        // 加载一下数据库的伤害
//        this.damageDataMap = new HsIslandBattleFieldDamageDataDaoImpl().getHsIslandDamageDataMap(activityId, groupId, battleFieldId, monsterId);
        // 如果是圣柱试炼，初始化一下商会积分榜单
        if (enemyType == HsIslandBattleFieldEnemyEnum.ENEMY_BOSS) {
            initUnionDamageRankModel();
        }
    }

    /**
     * 初始化一下榜单
     */
    private void initUnionDamageRankModel() {
        this.unionDamageRankModel = new CrossHsIslandRankListModel();
        Map<String, HsIslandRank> rankMap = new ConcurrentHashMap<>();
        for (HsIslandBattleFieldDamageData damageData : damageDataMap.values()) {
            HsIslandRank rank = rankMap.getOrDefault(damageData.getUnionId(), new HsIslandRank());
            rank.setRankId(damageData.getUnionId());
            rank.setValue(rank.getValue() + damageData.getTotalDamage());
            rank.setParam(new ArrayList<>());
            rank.setLastUpdateTime(Math.max(rank.getLastUpdateTime(), damageData.getUpdateTime()));
            rankMap.put(damageData.getUnionId(), rank);
        }
        unionDamageRankModel.batchRankChange(new ArrayList<>(rankMap.values()));
    }

    /**
     * 获取配置
     * @return
     */
    public HsIslandConfig getConfig() {
        return CrossHsIslandActivityMgr.getConfig(activityId, groupId);
    }

    /**
     * 获取活动
     * @return
     */
    public CrossHsIslandActivity getActivity() {
        return CrossHsIslandActivityMgr.getActivity(activityId, groupId);
    }


    /**
     * 添加伤害
     */
    public void addDamage(String unionId, long userId, long damage) {
        HsIslandBattleFieldDamageData damageData = this.damageDataMap.get(userId);
        if (damageData == null) {
            damageData = new HsIslandBattleFieldDamageData();
            damageData.setActivityId(activityId);
            damageData.setGroupId(groupId);
            damageData.setUnionId(unionId);
            damageData.setUserId(userId);
            damageData.setBattleFieldId(battleFieldId);
            damageData.setMonsterId(monsterId);
            damageData.setTotalDamage(0);
            damageData.setTimes(0);
            damageData.setUpdateTime(System.currentTimeMillis());
            damageData.setInsertOption();
            this.damageDataMap.put(userId, damageData);
        }
        damageData.setTotalDamage(damageData.getTotalDamage() + damage);
        damageData.setTimes(damageData.getTimes() + 1);
        damageData.setUpdateTime(System.currentTimeMillis());

        // 圣柱试炼下，更新下自己商会的伤害榜单
        if (enemyType == ENEMY_BOSS) {
            unionDamageRankChange(unionId);
        }
    }

    /**
     * 计算商会的总伤害
     *
     * @param unionId 商会id
     */
    public long calcUnionDamage(String unionId) {
        long totalDamage = 0;
        for (HsIslandBattleFieldDamageData damageData : damageDataMap.values()) {
            if (damageData.getUnionId().equals(unionId)) {
                totalDamage += damageData.getTotalDamage();
            }
        }
        return totalDamage;
    }

    /**
     * 获取这个商会最后一次攻击的时间
     */
    public long getUnionLastAttackTime(String unionId) {
        long lastAttackTime = 0;
        for (HsIslandBattleFieldDamageData damageData : damageDataMap.values()) {
            if (damageData.getUnionId().equals(unionId)) {
                lastAttackTime = Math.max(lastAttackTime, damageData.getUpdateTime());
            }
        }
        return lastAttackTime;
    }


    /**
     * 商会伤害变更榜单
     */
    public void unionDamageRankChange(String unionId) {
        if (enemyType != HsIslandBattleFieldEnemyEnum.ENEMY_BOSS) {
            return;
        }
        // 更新下榜单
        unionDamageRankModel.rankChange(new HsIslandRank(
                        unionId,
                        calcUnionDamage(unionId),
                        new ArrayList<>(),
                        getUnionLastAttackTime(unionId)
                )
        );
    }

    /**
     * 获取我的商会排名
     */
    public int getMyUnionRank(String unionId) {
        if (unionDamageRankModel == null) {
            return -1;
        }
        return unionDamageRankModel.getMyRank(unionId);
    }

    /**
     * 获取我的商会的伤害
     */
    public long getMyUnionDamage(String unionId) {
        if (unionDamageRankModel == null) {
            return 0;
        }
        HsIslandRank myRankInfo = unionDamageRankModel.getMyRankInfo(unionId);
        return myRankInfo == null ? 0 : myRankInfo.getValue();
    }

    public void save() {
        HsIslandBattleFieldDamageDataDaoImpl damageDataDao = new HsIslandBattleFieldDamageDataDaoImpl();
        for (HsIslandBattleFieldDamageData damageData : damageDataMap.values()) {
            if (damageData.isInsertOption()) {
                damageDataDao.add(damageData);
            } else if (damageData.isUpdateOption()) {
                damageDataDao.update(damageData);
            }
        }
    }

    public Map<Long, HsIslandBattleFieldDamageData> getDamageDataMap() {
        return damageDataMap;
    }

    public HsIslandBattleFieldDamageData getDamageData(long userId) {
        return damageDataMap.get(userId);
    }
}
